package com.example.userservice.controller;

import com.example.userservice.model.User;
import com.example.userservice.service.UserService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@CrossOrigin
public class UserController {
    @Resource
    UserService userService;

    @RequestMapping("/add")
    public String addUser(String userName, String userSecret, String userShowName, String userDetail){
        int user = userService.ifUserName(userName);
        if (user == 0){
            return "该用户名已经存在";
        }
        userService.addUser(userName,userSecret,userShowName,userDetail);
        return "添加成功";

    }

//    根据用户名称模糊查询
    @RequestMapping("/all")
    public List<User> selUserByName(String userName){

        if (userName.equals("")){
            return userService.allUser("");
        }else {
            return userService.allUser(userName);
        }

    }

//    显示所有用户信息
//    @RequestMapping("/all")
//    public List<User> allUser(){
//        return userService.allUser("");
//    }

//    删除一个或多个用户信息
    @RequestMapping("/delUsers")
    public String delUsers(@RequestParam(value="userName",required = false)List<String> userName){
        int i = userService.delUsers(userName);
        if(i==0){
            return "删除失败";
        }
        return "删除成功";
    }

//    修改用户信息页面
    @RequestMapping("/update")
    public User update(String userName){
        return userService.showUser(userName);
    }

//    修改用户信息
    @RequestMapping("/updateUser")
    public String updateUser(String userName,String userSecret, String userShowName, String userDetail){
        int i = userService.updateUser(userName,userSecret,userShowName,userDetail);
        if(i==0){
            return "修改失败";
        }
        return "修改成功";
    }
}
